package com.yanyeori.framework.storage.controller;

import com.yanyeori.framework.core.model.WebResponse;
import com.yanyeori.framework.storage.controller.vo.STSInfo;
import com.yanyeori.framework.storage.controller.vo.UrlSignInfo;
import com.yanyeori.framework.storage.service.StorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 文件中心接口
 *
 * @author chenkuan 2023-02-14
 */
@RestController
@RequestMapping("/storage")
public class StorageController {

    @Autowired
    private StorageService storageService;

    /**
     * STS模式-获取STS授权信息
     */
    @GetMapping("/getSTSInfo")
    public WebResponse<STSInfo> getSTSInfo() {
        return WebResponse.ok(storageService.getSTSInfo());
    }

    /**
     * 自签名模式-获取上传链接签名
     *
     * @param content 待签名内容
     */
    @GetMapping("/getSign")
    public WebResponse<UrlSignInfo> getSign(@RequestParam(value = "content", required = false) String content) {
        return WebResponse.ok(storageService.getSign(content));
    }

    /**
     * 获取上传链接
     *
     * @param name 文件名
     */
    @GetMapping("/getUploadUrl")
    public WebResponse<Map<String, String>> getUploadUrl(@RequestParam("name") String name) {
        return WebResponse.ok(storageService.getUploadUrl(name));
    }

    /**
     * 获取文件链接，默认有效期12小时
     *
     * @param key 文件key
     */
    @GetMapping("/getFileUrl")
    public WebResponse<String> getFileUrl(@RequestParam("key") String key) {
        return WebResponse.ok(storageService.getFileUrl(key));
    }

    /**
     * 获取文件自定义有效期的链接，单位分钟
     *
     * @param key  文件key
     * @param time 过期时间，单位分钟
     */
    @GetMapping("/getFileUrlWithTime")
    public WebResponse<String> getUrlWithTime(@RequestParam("key") String key,
                                              @RequestParam("time") long time) {
        return WebResponse.ok(storageService.getFileUrl(key, time, TimeUnit.MINUTES));
    }
}
